//
//  HMHomeModel.swift
//  swift微博项目
//
//  Created by huodian on 16/9/27.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

import YYModel

import SDWebImage


class HMHomeModel: NSObject {
    
    lazy var homeModelArray : [HMStatuesCellModel] = [HMStatuesCellModel]()
    
    func loadThedata(isPulling:Bool,finish:@escaping (Bool,Int)->()){
        
        
        let str = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        var paramara = ["access_token":HMUserAccessViewModel.shareUserAccess.userAccess?.access_token ?? ""]
        
//        print(HMUserAccessViewModel.shareUserAccess.userAccess?.access_token)
        
        if isPulling {
            
            let lv  = homeModelArray.last?.statues?.id ?? 0
            
            
            paramara["max_id"] = "\(lv - 1)"
            
        }else
        {
            
            let v = homeModelArray.last?.statues?.id ?? 0
            
            paramara["since_id"] = "\(v)"
            
        }
        
        
        HMNetWorking.shareNetWorking.requset(method: .GET, url: str, parameters: paramara) { (responsedObject, error ) in
            
            if error != nil {
                
//                SVProgressHUD.showError(withStatus: "你大爷的又没有网,信不信我砸了电脑")
                print("是1在着吗")
                
                finish(false,0)
                
                return
                
            }
            
            let res = responsedObject as! [String:Any]
           
            guard let array = res["statuses"] as? [[String:Any]] else
            {
                   print("是2在着吗")
                
                finish(false,0)
                
                return
            }
            
            var tempArray = [HMStatuesCellModel]()
            
        
            
            for modele in array{
                
//                let statues = HMStatuesModel(dict: modele)
//                
//                let statu = HMStatuesCellModel()
//                
//                statu.statues = statues
//          
//                
//                self.homeModelArray.append(statu)
                
                let viewmodel = HMStatuesCellModel()
                let s = HMStatuesModel()
                s.yy_modelSet(with: modele)
                viewmodel.statues = s
                //通过YYModel方式来字典转模型
                //              let s = HMStatus(dict: item)
                
                tempArray.append(viewmodel)
                
            }
            
            if isPulling {
                
                self.homeModelArray += tempArray
                
            }else
            {
                
                self.homeModelArray = (tempArray + self.homeModelArray)
                
            }
            
            
//            self.tableView.reloadData()
         
           // finish(true,tempArray.count)
            
            self.getTheSimpImage(tempArray: tempArray, simpImageFinish: finish)
            
        }
        
        
        
    }
    
    //监听图片下载是否成功
    func getTheSimpImage(tempArray:[HMStatuesCellModel],simpImageFinish:@escaping (Bool,Int)->()){
        
        let group = DispatchGroup.init()

        
        
        for item  in tempArray {
        
            if item.pictureInfo?.count == 1 {
                
                group.enter()
                
                let url = URL(string:item.pictureInfo?.first?.thumbnail_pic ?? "")
                
                
                SDWebImageManager.shared().downloadImage(with: url, options: [], progress: nil, completed: { (image , _, _, _, _) in
                    
                    print("单张图片下载完成")
                    
                    print(image?.size)
                    
                    group.leave()
                    
                })
                
                
                
            }
         
        }
        
        
       
        group.notify(queue: DispatchQueue.main, execute: {
            
            simpImageFinish(true,tempArray.count)
            
            print("所有单张图片打印完毕")
            
        })
        
    }
    
}
